package com.kinghunt.misp.service.impl.devicex;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.devicex.DeviceDTO;
import com.kinghunt.misp.common.dto.devicex.DeviceRepairDTO;
import com.kinghunt.misp.common.enums.devicex.DeviceTypeEnum;
import com.kinghunt.misp.common.param.devicex.DeviceRepairParam;
import com.kinghunt.misp.dal.dao.devicex.DeviceRepairDAO;
import com.kinghunt.misp.dal.model.devicex.DeviceRepairDO;
import com.kinghunt.misp.service.api.devicex.DeviceRepairService;
import com.kinghunt.misp.service.api.devicex.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class DeviceRepairServiceImpl implements DeviceRepairService {

    @Autowired(required = false)
    private DeviceRepairDAO deviceRepairDAO;
    @Autowired(required = false)
    private DeviceService deviceService;

    @Override
    public ApiResult<PageInfo<DeviceRepairDTO>> getDeviceRepairList(DeviceRepairParam deviceRepairParam) {
        int offset = (deviceRepairParam.getPageNum() - 1) * deviceRepairParam.getPageSize();
        DeviceRepairDO deviceRepairDO = ConvertUtil.convert(deviceRepairParam, DeviceRepairDO.class);
        int count = deviceRepairDAO.selectDeviceRepairListCount(deviceRepairDO);
        PageInfo<DeviceRepairDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(deviceRepairParam.getPageNum());
        pageInfo.setPageSize(deviceRepairParam.getPageSize());
        List<DeviceRepairDO> deviceRepairDOList = deviceRepairDAO.selectDeviceRepairList(deviceRepairDO, offset, deviceRepairParam.getPageSize());
        List<DeviceRepairDTO> dto =  ConvertUtil.convert(deviceRepairDOList, DeviceRepairDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<DeviceRepairDTO> getDeviceRepairDetail(Long repairId) {
        DeviceRepairDO deviceRepairDO = deviceRepairDAO.selectByPrimaryKey(repairId);
        DeviceRepairDTO deviceRepairDTO = ConvertUtil.convert(deviceRepairDO, DeviceRepairDTO.class);
        return ApiResult.ok(deviceRepairDTO);
    }

    @Override
    public ApiResult addDeviceRepair(DeviceRepairParam deviceRepairParam) {
        ApiResult<DeviceDTO> apiResult= deviceService.getDeviceDetail(deviceRepairParam.getDeviceId());
        if(!apiResult.isSuccess() || null == apiResult.getData() ){
            return ApiResult.failOfMessage("您申请维修的设备不存在！");
        }
        DeviceRepairDO deviceRepairDO = ConvertUtil.convert(deviceRepairParam, DeviceRepairDO.class);
        // 保存地址到数据库
        deviceRepairDAO.insert(deviceRepairDO);
        return ApiResult.ok();
    }
}
